myApp.controller('roleListController', function($scope, $http) {
  
  $scope.roleList = [];
  $http.get(python_url + '/api/v1/role/?token_id='+getTokenId()).success(function(data) {
    $scope.roleList = data.data;
  });


  $scope.total=$scope.roleList.length;
  $scope.page = {size: 15, index: 1};


  $scope.add = function(id) {
    window.location.href = "#/menu/roleAdd/" + id;
  };
});






myApp.controller('roleAddController', function($scope, $stateParams, $http) {
  $scope.id = $stateParams.id;
  $scope.roleinfo = {};
  $scope.roleinfo.role_name='';
  $scope.roleinfo.desc='';
  $scope.info = {};
  $scope.lock = false;




  $scope.goBack = function() {
    window.location.href = "#/menu/roleList/";
  };


  function createSubTree() {
    $.ajax({
      type: "GET",
      url: python_url + '/api/v1/menu/?token_id='+getTokenId(),
      async: false,
      success: function(data) {
        $scope.treedata = data.data;

      }
    });
  }


  createSubTree();
  $scope.treeOptions = {
    multiSelection: false,
    allowDeselect: false,
    nodeChildren: 'child_menu_list'
  };

  $scope.selectedNode = $scope.treedata[0];

  $scope.expandedNodes = [];

  function roleTree() {
    if ($scope.id != undefined) {
      $.ajax({
        type: "GET",
        url: python_url + '/api/v1/menu/?role_id=[' + $scope.id+']&token_id='+getTokenId(),
        async: false,
        success: function(data) {
          $scope.roleTree = data.data;

        }
      });
    }

  }
  if ($scope.id != 'undefined') {
    $http.get(python_url + '/api/v1/role/?role_id=' + $scope.id+'&token_id='+getTokenId()).success(function(data) {
      $scope.roleinfo = data.data[0];
      $scope.lock = true;
    });
    roleTree();
    angular.forEach($scope.treedata, function(treedata) {
      angular.forEach($scope.roleTree, function(roleTree) {
        if (treedata.menu_id == roleTree.menu_id) {
          treedata.Selected = true;
          $scope.expandedNodes.push(treedata);
        }
        angular.forEach(treedata.child_menu_list, function(data) {
          angular.forEach(roleTree.child_menu_list, function(tree) {
            if (data.menu_id == tree.menu_id) {
              data.Selected = true;
            }
          })

        })

      })
    });
  }
  var obj = [];
  $scope.save = function() {
    if($scope.roleinfo.role_name==''){
      alert('请输入角色!');
      return;
    }
    if($scope.roleinfo.desc==''){
      alert('请输入描述!');
      return;
    }
    var treedata = $scope.treedata;
    for (var i = 0; i < treedata.length; i++) {
      if (treedata[i].Selected == true) {
        obj.push(treedata[i].menu_id);

      }
      for (var j = 0; j < treedata[i].child_menu_list.length; j++) {

        if (treedata[i].child_menu_list[j].Selected == true) {
          obj.push(treedata[i].child_menu_list[j].menu_id);
        }
      }

    };
   if(obj.length==0){
    alert('请选择菜单!');
    return;
   };

    if ($scope.id == 'undefined') {
      $http.get(python_url + '/api/v1/role/add?token_id='+getTokenId()+'&role_name=' + $scope.roleinfo.role_name + '&desc=' + $scope.roleinfo.desc).success(function(data) {
        $scope.id = data.data.role_id;
        $scope.menu_power();
      });
    } else {
      $http.get(python_url + '/api/v1/role/update?token_id='+getTokenId()+'&desc=' + $scope.roleinfo.desc + '&role_id=' + $scope.id).success(function(data) {
        $scope.menu_power();
        window.location.href = "#/menu/roleList/";

      });
    }

    $scope.menu_power = function() {
      $http.get(python_url + '/api/v1/role/menu_power?token_id='+getTokenId()+'&menu_list=' + JSON.stringify(obj) + '&role_id=' + $scope.id).success(function(data) {
        window.location.href = "#/menu/roleList/";
      });
    }

  }
});